package cn.edu.zju.qsc.rouchseg.segment;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Splitter {
	public final Pattern notationPattern;
	public final Pattern latinPattern;
	
	public Splitter() {
		this.notationPattern = 
			Pattern.compile("[-\\\\»=-\\[\\].。，,！…!……《》<>\"':：？\\?、\\|“”‘’；—（）·\\(\\)　~～ ]");
		this.latinPattern = 
			Pattern.compile("([0-9A-Za-z\\-\\+#@_\\.]+)");
	}
	
	public List<String> split(String sentence) {
		/* 根据标点符号预切分 */
		List<String> sentences = new ArrayList<String>();
		String[] splittedTxt = this.notationPattern.split(sentence);
		/* 剔除英文字符 */
		String txt;
		for (int i = 0; i < splittedTxt.length; i++) {
			txt = splittedTxt[i];
			Matcher m = this.latinPattern.matcher(txt);
			int start = 0, end = 0;
			while (m.find()) {
				end = m.start();
				sentences.add(txt.substring(start, end));
				sentences.add(m.group());
				start = m.end();
			}
			if (start < txt.length()) {
				sentences.add(txt.substring(start));	/* 最后一段字符 */
			}
		}
		return sentences;
	}
}
